package com.orifine.orm.information;

import com.orifine.orm.Information;
import com.orifine.orm.TypeManager;
import com.orifine.orm.model.ColumnModel;

public class ValueListInfo implements Information {
	private Object[] values;
	private Information defaultInfo;
	private Class<?> typeClazz;
	private boolean isCycle = true;
	
	public ValueListInfo(Object[] values){
		this.values = values;
		this.isCycle = true;
	}
	
	public ValueListInfo(Object[] values, boolean isCycle){
		this.values = values;
		this.isCycle = isCycle;
	}
	
	public ValueListInfo(Object[] values, Class<?> typeClazz){
		this.values = values;
		this.typeClazz = typeClazz;
		this.isCycle = false;
	}
	
	public ValueListInfo(Object[] values, Information defaultInfo){
		this.values = values;
		this.defaultInfo = defaultInfo;
		this.isCycle = false;
	}
	
	@Override
	public Object value(int index, ColumnModel columnModel) {
		if(this.isCycle){
			return values[index % values.length];
		} else if(defaultInfo != null) {
			if(index < values.length){
				return values[index];
			} else {
				return defaultInfo.value(index, columnModel);
			}
		} else {
			if(index < values.length){
				return values[index];
			} else {
				return null;
			}
		}
	}

	@Override
	public void init(ColumnModel columnModel) {
		if(typeClazz != null){
			defaultInfo = TypeManager.getType(typeClazz).getDefaultInfo(columnModel);
		}
		if(defaultInfo != null){
			defaultInfo.init(columnModel);
		}
	}
	
}
